package com.yeyks.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.yeyks.service.OssService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.URL;
import java.util.Date;

@Service
public class OssServiceImpl implements OssService {

    private static String    endpoint = "oss-cn-hangzhou.aliyuncs.com";
    private static String    accessKey = "LTAITpv2oyvCJueR";
    private static String    accessKeySecret = "FC20WVDv4nHzQ77TQaZBzkgCsBYJB4";
    private static String    bucket = "yeyu-kuaisu";
    private        OSSClient ossClient;
    private static String    prefixUrl = "https://yeyu-kuaisu.oss-cn-hangzhou.aliyuncs.com/";

    public OssServiceImpl() throws Exception {
        ossClient = new OSSClient(endpoint, accessKey, accessKeySecret);
    }

    @Override
    public String getPrefixUrl() {
        return prefixUrl;
    }

    @Override
    public String uploadImage(InputStream inputStream, String key) {
        PutObjectResult putObjectResult = ossClient.putObject(bucket, key, inputStream);
        return getPrefixUrl() + key;
    }

    @Override
    public byte[] readOSS(String key) {
        InputStream is = null;
        try {
            OSSObject object = null;
            object = ossClient.getObject(bucket, key);
            if (object == null) {
                return null;
            }
            is = object.getObjectContent();
            return IOUtils.readStreamAsByteArray(is);
        } catch (Exception e) {
            return null;
        } finally {
            IOUtils.safeClose(is);
        }
    }

    @Override
    public boolean writeOSS(Object source, String key) {
        InputStream in = null;
        if (null == source) {
            return false;
        }
        try {
            in = getInputStream(source);
            ossClient.putObject(bucket, key, in);
            return true;
        } catch (Exception e) {
        } finally {
            IOUtils.safeClose(in);
        }
        return false;
    }

    public String generatePresignedUrl(String key, int expireSeconds) {
        long time = System.currentTimeMillis() + expireSeconds * 1000;
        try {
            URL url = ossClient.generatePresignedUrl(bucket, key, new Date(time));
            return url.toString();
        } catch (Exception e) {
        }
        return null;
    }

    private InputStream getInputStream(Object source) throws FileNotFoundException {
        InputStream in;
        if (source instanceof InputStream) {
            in = (InputStream) source;
        } else if (source instanceof byte[]) {
            in = new ByteArrayInputStream((byte[]) source);
        } else {
            in = new FileInputStream(source.toString());
        }
        if (null == in) {
            throw new RuntimeException("Object转文件流失败，找不到对应类型");
        }
        return in;
    }
}
